package com.alibaba.csp.sentinel.dashboard.config;

import com.alibaba.csp.sentinel.dashboard.constants.SentinelConStants;
import com.alibaba.nacos.api.config.ConfigFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

/**
 * @Description:
 * @Note:
 * @Author: zhangfei
 * @Date: 2025/2/25 14:49
 **/
@Component
@Slf4j
public class CustomerPageFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 初始化代码
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        if (httpRequest.getRequestURI().endsWith("/app/views/dashboard/home.html")) {
            try {
                ClassPathResource resource = new ClassPathResource("app/views/dashboard/home.html");
                File file = resource.getFile();
                if (file.exists()) {
                    byte[] content = Files.readAllBytes(file.toPath());
                    httpResponse.setContentType("text/html");
                    httpResponse.getOutputStream().write(content);
                    return;
                } else {
                    log.warn("Custom home.html not found at: " + file.getAbsolutePath());
                }
            } catch (IOException e) {
                e.printStackTrace();
                httpResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to load custom home.html");
                return;
            }
        }

        chain.doFilter(request, response);
    }

    /**
     * demo，向nacos新增配置
     * @param args
     * @throws NacosException
     */
    public static void main(String[] args) throws NacosException {
load();
    }

    public static void get() throws NacosException {
        String serverAddr = "localhost:8848"; // Nacos 服务器地址
        ConfigService configService = ConfigFactory.createConfigService(serverAddr);
        String rules = configService.getConfig("zffc-gateway-flow-rules.json",
                SentinelConStants.GROUP_ID, 3000);
    }

    public static void load() throws NacosException {
        String serverAddr = "localhost:8848"; // Nacos 服务器地址
        ConfigService configService = ConfigFactory.createConfigService(serverAddr);
        configService.publishConfig(
                "zffc-gateway-flow-rules.json", "DEFAULT_GROUP", "[\n" +
                        "    {\n" +
                        "        \"app\": \"zffc-gateway\",\n" +
                        "        \"clusterMode\": false,\n" +
                        "        \"controlBehavior\": 0,\n" +
                        "        \"count\": 10,\n" +
                        "        \"grade\": 1,\n" +
                        "        \"id\": 3,\n" +
                        "        \"limitApp\": \"default\",\n" +
                        "        \"maxQueueingTimeMs\": 500,\n" +
                        "        \"resource\": \"testResource\",\n" +
                        "        \"strategy\": 0,\n" +
                        "        \"warmUpPeriodSec\": 10\n" +
                        "    },\n" +
                        "    {\n" +
                        "        \"app\": \"zffc-gateway\",\n" +
                        "        \"clusterConfig\": {\n" +
                        "            \"acquireRefuseStrategy\": 0,\n" +
                        "            \"clientOfflineTime\": 2000,\n" +
                        "            \"fallbackToLocalWhenFail\": true,\n" +
                        "            \"resourceTimeout\": 2000,\n" +
                        "            \"resourceTimeoutStrategy\": 0,\n" +
                        "            \"sampleCount\": 10,\n" +
                        "            \"strategy\": 0,\n" +
                        "            \"thresholdType\": 0,\n" +
                        "            \"windowIntervalMs\": 1000\n" +
                        "        },\n" +
                        "        \"clusterMode\": false,\n" +
                        "        \"controlBehavior\": 0,\n" +
                        "        \"count\": 1,\n" +
                        "        \"gmtCreate\": 1740735302928,\n" +
                        "        \"gmtModified\": 1740735302928,\n" +
                        "        \"grade\": 1,\n" +
                        "        \"id\": 4,\n" +
                        "        \"ip\": \"192.168.68.108\",\n" +
                        "        \"limitApp\": \"default\",\n" +
                        "        \"port\": 8719,\n" +
                        "        \"resource\": \"1\",\n" +
                        "        \"strategy\": 0\n" +
                        "    }\n" +
                        "]");

    }

    @Override
    public void destroy() {
        // 清理代码
    }
}
